//
//  SFOriginView.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/12.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit
import SnapKit

class SFOriginView: UIView {


    var myBottomeConstraint: Constraint?

    var statusViewModel: SFStatusViewModel?{


        didSet{


            //给控件设置数据

            //头像
            if let urlString = statusViewModel?.status?.user!.profile_image_url , let url = NSURL(string: urlString){

                self.iconView.sd_setImageWithURL(url)
            }
            //名字
            userName.text = statusViewModel?.status?.user?.screen_name
            //来源
            sourceLabel.text = statusViewModel?.dealSource
            //时间
            timeLabel.text = statusViewModel?.creatTimeText
            //加V
            verifiedImage.image = statusViewModel?.verifiedImage
            //会员等级
            vipImageView.image = statusViewModel?.vipImage
            //text
            contentLabel.text = statusViewModel?.status?.text


            //删除原来的底部布局
            self.myBottomeConstraint?.uninstall()

            //通过数据有没有配图判断显不显示配图控件
            if statusViewModel?.status?.pic_urls != nil && statusViewModel?.status?.pic_urls?.count > 0 {

                //设置数据
                pictureView.pic_urls = statusViewModel?.status?.pic_urls
                //取消隐藏
                pictureView.hidden = false

                //修改底部约束
                self.snp_makeConstraints(closure: { (make) in
                    self.myBottomeConstraint = make.bottom.equalTo(pictureView).offset(SFStatusCellMargin).constraint
                })

            } else {

                //隐藏
                pictureView.hidden = true
                //修改底部约束
                self.snp_makeConstraints(closure: { (make) in
                    self.myBottomeConstraint = make.bottom.equalTo(contentLabel).offset(SFStatusCellMargin).constraint
                })

            }

        }

    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){


        //添加控件
        addSubview(iconView)
        addSubview(userName)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImage)
        addSubview(vipImageView)
        addSubview(contentLabel)
        addSubview(pictureView)

        //设置布局

        //头像
        iconView.snp_makeConstraints { (make) in

            make.top.leading.equalTo(self).offset(SFStatusCellMargin)
//            make.size.equalTo(CGSizeMake(SFStatusCellHeadImageWH, SFStatusCellHeadImageWH))
            make.size.equalTo(CGSize(width: SFStatusCellHeadImageWH, height: SFStatusCellHeadImageWH))

        }

        //名字
        userName.snp_makeConstraints { (make) in

            make.leading.equalTo(iconView.snp_trailing).offset(SFStatusCellMargin)
            make.top.equalTo(iconView)
        }
        //发布时间
        timeLabel.snp_makeConstraints { (make) in

            make.leading.equalTo(userName)
            make.bottom.equalTo(iconView)
        }
        //微博来源
        sourceLabel.snp_makeConstraints { (make) in

            make.bottom.equalTo(iconView)
            make.leading.equalTo(timeLabel.snp_trailing).offset(SFStatusCellMargin)
        }
        //vip标示
        vipImageView.snp_makeConstraints { (make) in

            make.top.equalTo(userName)
            if timeLabel.text?.characters.count > 0{
                make.leading.equalTo(userName.snp_trailing).offset(SFStatusCellMargin)
            }else {
                make.leading.equalTo(iconView.snp_trailing).offset(SFStatusCellMargin)
            }
        }

        //加v认证
        verifiedImage.snp_makeConstraints { (make) in

            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }

        //微博文字
        contentLabel.snp_makeConstraints { (make) in

            make.top.equalTo(iconView.snp_bottom).offset(SFStatusCellMargin)
            make.leading.equalTo(iconView)
            make.trailing.equalTo(self).offset(-SFStatusCellMargin)
        }

        //原创微博配图
        pictureView.snp_makeConstraints { (make) in

            make.top.equalTo(contentLabel.snp_bottom).offset(SFStatusCellMargin)
            make.leading.equalTo(contentLabel)
//            make.width.height.equalTo(CGSizeMake(100, 100))

        }

        self.snp_makeConstraints { (make) in


            self.myBottomeConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(SFStatusCellMargin).constraint

        }


    }


    //MARK: - 懒加载控件

    //头像
    lazy var iconView: UIImageView = {

        let img = UIImageView(image: UIImage(named: "avatar_default_small"))

        img.sizeToFit()

        return img

    }()
    //名字
    lazy var userName: UILabel = {

        let label = UILabel()

        label.text = "隔壁的番茄阿姨"

        label.font = UIFont.systemFontOfSize(15)
        label.sizeToFit()

        return label
    }()

    //发布时间
    lazy var timeLabel: UILabel = {

        let label = UILabel()

        label.text = "一年前"
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.orangeColor()
        label.sizeToFit()

        return label
    }()

    //微博来源
    lazy var sourceLabel: UILabel = {

        let label = UILabel()

        label.text = "来自iphone6"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.blackColor()
        label.sizeToFit()

        return label

    }()

    //加V认证
    lazy var verifiedImage: UIImageView = {

        let imgV = UIImageView(image: UIImage(named: "avatar_vip"))

        imgV.sizeToFit()

        return imgV

    }()

    //会员图标
    lazy var vipImageView: UIImageView = {

        let img = UIImageView(image: UIImage(named: "common_icon_membership_level1"))

        img.sizeToFit()

        return img

    }()

    //显示微博文字
    lazy var contentLabel: UILabel = {

        let label = UILabel()

        label.text = "你还记得吗,记忆的炎夏"

        label.numberOfLines = 0

        label.font = UIFont.systemFontOfSize(15)

        label.sizeToFit()

        return label
        
    }()

    //配图view
    lazy var pictureView: SFStatusPictureView = {

        let p = SFStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

        

        return p
    }()

}
